package com.epfresh.supplier.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.*;
import com.epfresh.supplier.entity.BookInfoEntity;
import java.util.List;
import com.epfresh.api.utils.*;
/**
 * Created by zzq on 2016/5/6.
 */
public class BookBestAdapter extends BaseAdapter{//} implements View.OnClickListener{

    int colors[] = {Color.parseColor("#b991e5"),Color.parseColor("#ff7b00"),Color.parseColor("#ffb234"),Color.parseColor("#ffe851"),Color.parseColor("#64d8bc"),Color.parseColor("#40b3f6"),Color.parseColor("#84e3ef")};

    public Context context;

    public Double colorLengthFlag;

    public List<BookInfoEntity.ProductListBean> list;

    int maxLength;


    private View.OnClickListener onItemClickListener;

    public BookBestAdapter(Context context , List<BookInfoEntity.ProductListBean> list) {
        this.context = context;
        this.list = list;
        maxLength = LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(40);
    }

    public View.OnClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_book_best, null, false);

            vh.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            vh.iv_background = convertView.findViewById(R.id.iv_background);
            vh.ll_best_item = convertView.findViewById(R.id.ll_best_item);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if(position==0){
            colorLengthFlag = list.get(0).getPrice()*1.1;
        }

        ViewGroup.LayoutParams lp;
        lp = vh.iv_background.getLayoutParams();

        BookInfoEntity.ProductListBean data = list.get(position);
        vh.tv_money.setText(FormatUtil.subZeroAndDot(""+data.getPrice()));

        vh.tv_name.setText(data.getName());
        GradientDrawable mGrad = (GradientDrawable) vh.iv_background.getBackground();
        if(position<colors.length)
            mGrad.setColor(colors[position]);
        else
            mGrad.setColor(colors[6]);

        lp.width = (int)(maxLength*(data.getPrice()/colorLengthFlag));

//        vh.iv_background.setLayoutParams(lp);

        vh.ll_best_item.setTag(R.id.item_key_position, position);
        vh.ll_best_item.setOnClickListener(onItemClickListener);

        return convertView;
    }


/*    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer){
            if (onItemClickListener != null) {
                int position = (int) o;
*//*                Intent detailIntent = new Intent(context,
                        BookGoodsYearActivity.class);
                detailIntent.putExtra("timeTitle", ((BookActivity)context).);
                detailIntent.putExtra("timeValue", list.get(position).getQueryDate());
                detailIntent.putExtra("money", data.getQueryTotleIncome());
                startActivity(detailIntent);*//*
                onItemClickListener.onItemClick(null, v, position, position);
            }
        }
    }*/

    public class ViewHolder {

        TextView tv_name;
        TextView tv_money;

        View iv_background,ll_best_item;

    }
}
